package com.lkm.app_ui.ui;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.lkm.app_ui.AppConst;
import com.lkm.app_ui.MBaseFragment;
import com.lkm.app_ui.R;
import com.lkm.app_ui.RouterUINavigation;
import com.lkm.app_ui.RunTimeData;
import com.lkm.app_ui.help.TaskHelp;
import com.lkm.app_ui.netapi.AppApi;
import com.lkm.comlib.help.ImageViewLoadHelp;
import com.lkm.comlib.help.ViewUIHelp;
import com.lkm.comlib.task2.DefaultTaskExecutor;
import com.lkm.frame.otto.LinkBusCycleWrapper;
import com.lkm.frame.otto.ObserverAutoNext;
import com.lkm.frame.task2.Task;
import com.lkm.frame.task2.TaskAdapter;
import com.lkm.frame.util.CycleHelp;
import com.lkm.market2_0.entity.ResponEntity;
import com.lkm.market2_0.m.steelyardserver.OnSteelyardKeyEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;



public class InfoQrFragment extends MBaseFragment implements View.OnClickListener {


    public static InfoQrFragment getInstance() {
        InfoQrFragment f = new InfoQrFragment();





        return f;
    }

    @Override
    protected int configLayoutResID() {
        return R.layout.fragment_infoqr;
    }

    @Override
    protected boolean configIsLockKey() {
        return true;
    }


    private ImageViewLoadHelp payImageViewLoadHelp;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        payImageViewLoadHelp = new ImageViewLoadHelp(getActivity(), Math.max(getResources().getDisplayMetrics().heightPixels, getResources().getDisplayMetrics().widthPixels), holdCycleHelp());
        payImageViewLoadHelp.setHasImagScaleType(ImageView.ScaleType.CENTER_INSIDE);
        payImageViewLoadHelp.setLoadingImagScaleType(ImageView.ScaleType.CENTER_INSIDE);
        payImageViewLoadHelp.setNotImagScaleType(ImageView.ScaleType.CENTER_INSIDE);


        getQR();

        reOnKeyEvent();

        for (int i = 0; i < 10; i++) {
            View v = getView().findViewWithTag("fun_" + i);
            if (v != null)
                v.setOnClickListener(this);
        }

    }

    private void reOnKeyEvent() {
        LinkBusCycleWrapper.register(new ObserverAutoNext<OnSteelyardKeyEvent>() {
            @Override
            public Class<OnSteelyardKeyEvent> getBusEventClass() {
                return OnSteelyardKeyEvent.class;
            }

            @Override
            public void onReceiver(OnSteelyardKeyEvent data) {
                if (!data.isIntercepted()) {
                    data.setIntercepted(true);
                    try {
                        View v = getView().findViewWithTag("fun_" + data.getKeyCode());
                        if (v != null)
                            onClick(v);
                    } catch (Exception e) {
                    }
                }

            }
        }, holdCycleHelp(), CycleHelp.CycleType.Destroy);
    }

    @Override
    public void onClick(View v) {
        if ("fun_1".equals(v.getTag())) {
            if(clearAllDialog!=null){clearAllDialog.dismiss();}
            getQR();
        }else
        if ("fun_9".equals(v.getTag())) {
            RouterUINavigation.getInstance().dismiss(InfoQrFragment.this);
        }
    }


    private void getQR() {
        if (isExit()) return;
        TaskHelp.cancelById(GetQRTask.class.getName());
        Task task = new GetQRTask();
        task.action();
        task.setParam(null);
        task.execTask();
        holdTaskCollection().add(task.getSerialNum());

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(clearAllDialog!=null){
            clearAllDialog.dismiss();
        }
    }

    private Dialog clearAllDialog;

    private class GetQRTask extends TaskAdapter<Void, String, ResponEntity<String> > {
        public GetQRTask() {
            super(new DefaultTaskExecutor(AppConst.getTaskExecutor(), true), GetQRTask.class.getName(), AppConst.getTaskManager2());
        }

        @Override
        public void onPreExecute() {
            super.onPreExecute();
            ViewUIHelp.showProgressBar(getActivity(), "加载信息...", GetQRTask.class.getName());
        }

        @Override
        public void doExecuting(Void param) {
            super.doExecuting(param);
            mTaskExecutor.executor(new Runnable() {
                @Override
                public void run() {
                    ResponEntity<String> responEntity = ResponEntity.fromJson(AppApi.stall_qr(GetQRTask.this), String.class, "");
                    complete(responEntity);
                }
            });
        }
        @Override
        public void onFinish(ResponEntity<String> responEntity) {
            super.onFinish(responEntity);
            if (responEntity.isSuccess()) {
                payImageViewLoadHelp.setImage(((ImageView) findViewById(R.id.img_qr_ali)), responEntity.getData());
                ViewUIHelp.showTips(getActivity(), "加载成功");
            } else {
                if (clearAllDialog == null) {
                    clearAllDialog = ViewUIHelp.showAlertTipsDialog(getActivity(), "加载信息失败。", "重新加载（按1）", null);
                    clearAllDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                        @Override
                        public void onDismiss(DialogInterface dialog) {
                            clearAllDialog = null;
                        }
                    });
                }
            }
        }
        @Override
        public boolean onInterceptEnd(boolean isCancel, ResponEntity<String> responEntity) {
            ViewUIHelp.disProgressBar(GetQRTask.class.getName());
            return super.onInterceptEnd(isCancel, responEntity);
        }
    }



}
